function http(url, data, success, error, method="POST") {
  var xhr = new XMLHttpRequest();
  xhr.open(method, url, true)
  xhr.withCredentials = true;  // 允许发送凭证
  xhr.setRequestHeader('X-CSRFToken', Cookies.get('csrftoken'))
  xhr.onreadystatechange = function() {
    if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
      // console.log(xhr.responseText)
      var res = JSON.parse(xhr.responseText)
      if(res.error_code === 0 || res.code === 200){
        success(res)
      }else{
        error(res)
      }
    }
  }
  xhr.timeout = 20 * 1000;
  xhr.ontimeout = function() {
    console.log('请求超时')
    error({error_code: 1, msg: '请求超时'})
  }
  xhr.onabort = function() {
    console.log('请求中断')
    error({error_code: 1, msg: '请求中断'})
  }
  xhr.onerror = function() {
    console.log('请求错误')
    error({error_code: 1, msg: '请求错误'})
  }
  xhr.onloadend = function() {
    // console.log('请求完成')
  }
  xhr.onprogress = function(e) {
    // console.log('请求进度', e)
  }
  // console.log(data)
  if (data instanceof FormData) {
    xhr.setRequestHeader('Content-type', 'multipart/form-data');
  } else {
    xhr.setRequestHeader('Content-type', 'application/json');
    data = JSON.stringify(data)
  }
  xhr.send(data)

}

function _post(url, data, success, error) {
  http(url, data, success, error, "POST")
}

function _get(url, data, success, error) {
  http(url, data, success, error, "GET")
}

function upload(data, url, success, error, onprogress) {
  var xhr = new XMLHttpRequest();
  xhr.open('POST', url, true)
  // xhr.setRequestHeader('X-CSRFToken', Cookies.get('csrftoken'))
  xhr.upload.onprogress = function (e) {
    console.log(e)
    onprogress(e)
  }
  xhr.onreadystatechange = function() {
    if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
      console.log(xhr.responseText)
      var res = JSON.parse(xhr.responseText)
      if(res.error_code === 0){
        success(res)
      }else{
        error(res)
      }
    }
  }
  xhr.send(data)
}

function showErr(id, msg) {
  document.getElementById(id + '-msg').innerText = msg
}

function countdown(ts) {
  ts = parseInt(ts)
  var endTime = new Date(ts).getTime();
  var now = new Date().getTime()/1000;
  var h = parseInt((endTime - now) / 3600);
  var m = parseInt((endTime - now - h * 3600) / 60);
  var s = parseInt(endTime - now - h * 3600 - m * 60);
  return {
    h: h,
    m: m,
    s: s
  }
}

var models = {
  envModel: {
    pre: "/ttp/object/environment",
    list: function(data, success, error) {
      _get(this.pre + 's', data, success, error)
    },
    create: function(data, success, error) {
      _post('/ttp/object/environment/create', data, success, error)
    },
    update: function(data, success, error) {
      _post('/ttp/object/environment/update/' + data.id, data, success, error)
    },
    batchRem: function(data, success, error) {
      _post(this.pre + '/batch/remove', data, success, error)
    },
    item: function(data, success, error) {
      _get('/ttp/object/environment/' + data, {}, success, error)
    }
  },
  insModel: {
    action: function(data, success, error) {
      _post('/ttp/api/stu/instance/action/', data, success, error)
    }
  },
  paperModel: {
    item: function(data, success, error) {
      get('/ttp/api/stu/paper/' + data, {}, success, error)
    },
    create: function(data, success, error) {
      _post('/ttp/api/stu/paper/create/', data, success, error)
    },
    choiceAnswer: function(data, success, error) {
      _post('/ttp/api/stu/choice/answer/', data, success, error)
    },
    blankAnswer: function(data, success, error) {
      _post('/ttp/api/stu/blank/answer/', data, success, error)
    },
    submit: function(data, success, error) {
      _post('/ttp/api/stu/paper/submit/', data, success, error)
    }
  },
  experimentModel: {
    pre: '/ttp/api/stu/experiment',
    pre1:'/ttp/experiment',
    list: function (data, success, error) {
      _get(this.pre + 's', data, success, error)
    },
    item: function(id, success, error) {
      _get(this.pre + '/' + id, {}, success, error)
    },
    report: function(id, success, error) {
      _get('/ttp/stu/report/' + id, {}, success, error)
    },
    createReport: function(data, success, error) {
      _post('/ttp/api/stu/report/create/', data, success, error)
    },
    recordScreen: function(data, success, error) {
      _post('ttp/stu/experiment/record/', data, success, error)
    },
    paginationList: function(id, success, error) {
      _get(this.pre1+'/chapter/'+id,{}, success, error)
    },
  },
  vmTransFileModel: {
    fileList: function (data, success, error) {
      _get(`ttp/api/stu/experiment/filetransfer-station-files?ip=${data.ip}`, {}, success, error)
    },
    exportList: function (data, success, error) {
      _get(`http://${data.ip}:7079/file-download?fileName=` + data.files, {}, success, error)
    },
  }
}
 